package com.young.commom.httpclient.okhttp;

import com.young.commom.httpclient.RequestHeaderInterceptor;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.internal.http.RealInterceptorChain;

import java.io.IOException;
import java.util.Map;

/**
 * @description：
 * @author: yangyahui01
 * @date: 11/29/24 1:04 PM
 */
public class OKHttpHttpRequestInterceptor extends RequestHeaderInterceptor implements Interceptor {
    public OKHttpHttpRequestInterceptor() {
    }

    public Response intercept(Chain chain) throws IOException {
        RealInterceptorChain realChain = (RealInterceptorChain)chain;
        Request request = realChain.request();
        Map<String, String> headers = this.requestHeaders();
        if (headers != null) {
            Request.Builder builder = request.newBuilder();
            headers.forEach(builder::header);
            request = builder.build();
        }

        return chain.proceed(request);
    }
}
